package when3meet.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import client.ServerAccess;

import when3meet.model.ClientInfo;
import when3meet.utilities.InputUtilities;
import when3meet.view.Application;
import when3meet.view.CreateEventFrame;
import when3meet.view.SignInFrame;
import xml.Message;

/**
 * Deal with GUI-based action, send create event request. Handle create event
 * response from server
 * 
 * @author Xi
 * 
 */
public class CreateEventController implements ActionListener {

	CreateEventFrame createEventFrame = null;
	JTextField eventNameField;
	JTextField maxEdgeField;
	JTextField maxChoiceField;
	JRadioButton openEventRBtn;
	JRadioButton closeEventRBtn;
	JRadioButton asynchronousRBtn;
	JRadioButton roundrobinRBtn;
	SignInFrame signInFrame = null;
	ServerAccess access;

	public CreateEventController(CreateEventFrame cef) {

		createEventFrame = cef;

	}

	public void actionPerformed(ActionEvent arg0) {

		// check event name input
		boolean b1 = InputUtilities.checkTextFieldInput(createEventFrame.getEventNameField());
		if (b1 == false) {
			createEventFrame.getEventNameField().setBackground(Color.RED);
		} else {
			createEventFrame.getEventNameField().setBackground(Color.WHITE);
		}

		// check max edges input
		boolean b2 = InputUtilities.checkIntegerInput(createEventFrame.getMaxEdgeField());
		if (b2 == false) {
			createEventFrame.getMaxEdgeField().setBackground(Color.RED);
		} else {
			createEventFrame.getMaxEdgeField().setBackground(Color.WHITE);
		}

		// check max choices input
		boolean b3 = InputUtilities.checkIntegerInput(createEventFrame.getMaxChoiceField());
		if (b3 == false) {
			createEventFrame.getMaxChoiceField().setBackground(Color.RED);
		} else {
			createEventFrame.getMaxChoiceField().setBackground(Color.WHITE);
		}

		if (b1 == true && b2 == true && b3 == true) {

			// send XML request
			sendRequest();
		}
	}

	/**
	 * send xml request
	 */
	public void sendRequest() {

		ClientInfo ci = ClientInfo.getInstance();
		// store moderator info

		ci.setModName(ci.getUserName());
		ci.setModPass(ci.getPassword());

		if (createEventFrame.getEventType().equals("open")) {

			String xmlString = Message.requestHeader() + "<createRequest " + "behavior = '"
					+ createEventFrame.getEventBehavior() + "' " + "type = '" + createEventFrame.getEventType() + "' "
					+ "question = '" + createEventFrame.getEventNameField().getText() + "' " + "numChoices = '"
					+ createEventFrame.getMaxChoiceField().getText() + "' " + "numRounds = '"
					+ createEventFrame.getMaxEdgeField().getText() + "'>" + "<user " + "name = '" + ci.getUserName()
					+ "' " + "password = '" + ci.getPassword() + "'/>" + "</createRequest>" + "</request>";

			Message m = new Message(xmlString);

			access = Application.getInstance().getServerAccess();
			access.sendRequest(m);
		}
		// for closed event, send choices set by moderator
		else if (createEventFrame.getEventType().equals("closed") || createEventFrame.getEventType() == "closed") {

			String choicesStr = "";

			String[] choices = ClientInfo.getInstance().getChoices();

			for (int i = 0; i < choices.length; i++) {
				String choiceXMLStr = "";
				choiceXMLStr = choices[i];
				choiceXMLStr = "<choice " + "value = '" + choiceXMLStr + "' " + "index ='" + i + "'/>";
				choicesStr = choicesStr + choiceXMLStr;
			}

			String xmlString = Message.requestHeader() + "<createRequest " + "behavior = '"
					+ createEventFrame.getEventBehavior() + "' " + "type = '" + createEventFrame.getEventType() + "' "
					+ "question = '" + createEventFrame.getEventNameField().getText() + "' " + "numChoices = '"
					+ createEventFrame.getMaxChoiceField().getText() + "' " + "numRounds = '"
					+ createEventFrame.getMaxEdgeField().getText() + "'>" + choicesStr + "<user " + "name = '"
					+ ci.getUserName() + "' " + "password = '" + ci.getPassword() + "'/>" + "</createRequest>"
					+ "</request>";

			Message m = new Message(xmlString);
			access = Application.getInstance().getServerAccess();
			access.sendRequest(m);

		}

	}

	// process xml respond from server
	public void process(Message response) {

		Node update = response.contents.getFirstChild();
		NamedNodeMap map = update.getAttributes();
		String id = map.getNamedItem("id").getNodeValue();

		ClientInfo ci = ClientInfo.getInstance();
		ci.setEventID(id);
		//
		createEventFrame = CreateEventFrame.getInstance();
		createEventFrame.dispose();
		signInFrame = SignInFrame.getInstance();
		signInFrame.setVisible(true);
		AddCloseEventChoiceBtnController.disposeFrame();

		// // preset event id at signInFrame
		signInFrame.getEventIDField().setText(id);
		// the moderator should sign in automatically to avoid THE BUG, which
		// kills at presentation day!
		String xmlString = Message.requestHeader() + "<signInRequest " + "id = '" + ci.getEventID() + "'>" + "<user "
				+ "name = '" + ci.getModName() + "' " + "password = '" + ci.getModPass() + "'/>" + "</signInRequest>"
				+ "</request>";

		Message m = new Message(xmlString);

		access = Application.getInstance().getServerAccess();
		access.sendRequest(m);
	}
}
